package com.icim.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.icim.csot.exporter.tentity.CsotHistoryTEntity;

public class TxtFileUtil {

    private static final String ENTER_KEY = "\r\n";
    private static final List<String> columnList = new ArrayList<String>();
    static {
        columnList.add("FACTORY");
        columnList.add("PANEL_ID");
        columnList.add("CSOT_PANEL_ID");
        columnList.add("PALLET_ID");
        columnList.add("BOX_ID");
        columnList.add("SOURCE_PALLET_ID");
        columnList.add("BLOCK_STATUS");
        columnList.add("SOURCE_BOX_ID");
        columnList.add("EVENT_TIMEKEY");
        columnList.add("EVENT_TIME");
        columnList.add("EVENT_NAME");
        columnList.add("EVENT_USER_ID");
        columnList.add("EVENT_COMMENT");
        columnList.add("PO_TYPE");
        columnList.add("PO_ID");
        columnList.add("PRODUCT_GROUP");
        columnList.add("MODEL_NAME");
        columnList.add("PRODUCT_ID");
        columnList.add("OWNER_ID");
        columnList.add("PRODUCT_VER");
        columnList.add("SOURCE_PRODUCT_ID");
        columnList.add("CELL_GRADE");
        columnList.add("GRADE");
        columnList.add("OPER_CODE");
        columnList.add("LINE_ID");
        columnList.add("EQP_ID");
        columnList.add("REWORK_CNT");
        columnList.add("REASON_CODE_TYPE");
        columnList.add("REASON_CODE");
        columnList.add("DEFECT_GROUP");
        columnList.add("DEFECT_CODE");
        columnList.add("DEFECT_QTY");
        columnList.add("ABNORMAL_FLAG");
        columnList.add("SLIM_FLAG");
    }

    public static void entity2Txt(List<CsotHistoryTEntity> tEntityList, String fileName, String beginTime, String endTime) throws Exception {
        StringBuffer buffer = new StringBuffer();
        buffer.append("--TABEL_NAME:BLOCK_HIST").append(ENTER_KEY);
        buffer.append("--START_TIME:").append(beginTime).append(ENTER_KEY);
        buffer.append("--END_TIME:").append(endTime).append(ENTER_KEY);
        buffer.append("--FACTORY:WA").append(ENTER_KEY);
        buffer.append("--ROW_COUNT:").append(tEntityList.size()).append(ENTER_KEY);
        buffer.append("--").append(ENTER_KEY);
        buffer.append("COLUMN_NAME:");
        for (String column : columnList) {
            buffer.append(column);
            if (columnList.indexOf(column) < columnList.size() - 1) {
                buffer.append(",");
            }
        }
        buffer.append(ENTER_KEY);
        for (CsotHistoryTEntity tEntity : tEntityList) {
            for (String column : columnList) {
                buffer.append(getEntityPropertyValue(tEntity, column));
                if (columnList.indexOf(column) < columnList.size() - 1) {
                    buffer.append("\t");
                }
            }
            buffer.append(ENTER_KEY);
        }
        buffer.append("--END:END");

        FileUtil.sting2File(fileName, buffer.toString());
    }

    private static String getEntityPropertyValue(CsotHistoryTEntity tEntity, String properName)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
            SecurityException {
        Class clazz = tEntity.getClass();
        Object str = null;
        Method method = clazz.getMethod("get" + properName, null);
        str = method.invoke(tEntity, null);
        System.out.println(str);
        return str == null ? "" : (String)str;
    }

    public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, NoSuchMethodException, SecurityException {
        CsotHistoryTEntity t = new CsotHistoryTEntity();
        t.setEQP_ID("EQP");
        TxtFileUtil.getEntityPropertyValue(t, "EQP_ID");
    }

}
